import { PublicInterface } from '@/interface/public';

/**
 * @name 操作对象
 */

/**
 * 根据原对象设置新对象值
 * @param obj 原对象
 * @param str 新对象值
 * @returns 新的对象
 */
export const objSetNewObjValue = (obj: PublicInterface.UnknownObjProps, str: string) => {
  const arr = str.split(',');
  let newObj: any = {};
  for (let key of arr) {
    newObj[key] = obj[key];
  }
  return newObj;
};

/**
 * @description 将对象内的key添加指定前缀
 */
export function objAddPrefix(obj: object = {}, flag: string = 'm_') {
  if (!obj) return {};
  const newObj: any = {};
  Object.keys(obj).forEach(key => {
    if (isValidKey(key, obj)) {
      newObj[flag + key] = obj[key];
    }
  });

  return newObj;
}
